package com.tanxin.bill.modules;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.EditText;

import com.allen.library.SuperTextView;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.blankj.utilcode.constant.TimeConstants;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.tanxin.bill.R;
import com.tanxin.bill.common.base.BaseEvent;
import com.tanxin.bill.common.base.BaseTitleActivity;
import com.tanxin.bill.common.data.BillData;
import com.tanxin.bill.common.data.CustomerData;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;
import io.objectbox.Box;

@SuppressLint("SimpleDateFormat")
public class AddBillActivity extends BaseTitleActivity {


    @BindView(R.id.ed_money)
    EditText edMoney;
    @BindView(R.id.tv_member)
    SuperTextView tvMember;
    @BindView(R.id.tv_type)
    SuperTextView tvType;
    @BindView(R.id.tv_start_time)
    SuperTextView tvStartTime;
    @BindView(R.id.tv_handle_time)
    SuperTextView tvHandleTime;

    private TimePickerBuilder startPicker;//开始 日期选择器
    private TimePickerBuilder handlePicker;//处理 日期选择器

    private CustomerData customerData;
    private BillData.Type type;
    private static final int CHECK_MEMBER = 1000;

    private Box<BillData> billBox;
    private Box<CustomerData> customerDataBox;

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, AddBillActivity.class);
        context.startActivity(intent);
    }


    @Override
    protected int bindLayout() {
        return R.layout.add_bill_activity;
    }

    @Override
    protected void getBundleExtras(Bundle extras) {

    }


    @Override
    protected void initView(Bundle savedInstanceState) {
        billBox = MyApp.getApplication().getBoxStore().boxFor(BillData.class);
        customerDataBox = MyApp.getApplication().getBoxStore().boxFor(CustomerData.class);
        initPicker();
        tvStartTime.setRightString(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")));
        tvHandleTime.setRightString(TimeUtils.date2String(TimeUtils.getDateByNow(7, TimeConstants.DAY),new SimpleDateFormat("yyyy-MM-dd")));
    }

    @Override
    protected void onEventComing(BaseEvent event) {

    }

    @Override
    protected String setTitle() {
        return "添加账单";
    }


    @OnClick({R.id.tv_member, R.id.tv_type, R.id.tv_start_time, R.id.tv_handle_time, R.id.btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_member:
                CustomerListActivity.startActivityForResut(this,1,CHECK_MEMBER);
                break;
            case R.id.tv_type:
                final String[] items = new String[] { "借款", "还钱"};

                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("选择类别").setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        tvType.setRightString(items[i]);
                        if (i==0){
                            type = BillData.Type.LOAN;
                            tvStartTime.setRightString(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")));
                            tvHandleTime.setRightString(TimeUtils.date2String(TimeUtils.getDateByNow(7, TimeConstants.DAY),new SimpleDateFormat("yyyy-MM-dd")));
                        }else {
                            type = BillData.Type.REPAY;
                            tvStartTime.setRightString(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")));
                            tvHandleTime.setRightString(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")));
                        }
                    }
                }).create().show();
                break;
            case R.id.tv_start_time:
                if (startPicker!=null){
                    KeyboardUtils.hideSoftInput(this);
                    startPicker.build().show();
                }
                break;
            case R.id.tv_handle_time:
                if (handlePicker!=null){
                    KeyboardUtils.hideSoftInput(this);
                    handlePicker.build().show();
                }
                break;
            case R.id.btn_save:
                String money = edMoney.getText().toString().trim();
                Date startTime = TimeUtils.string2Date(tvStartTime.getRightString(),new SimpleDateFormat("yyyy-MM-dd"));
                Date handleTime = TimeUtils.string2Date(tvHandleTime.getRightString(),new SimpleDateFormat("yyyy-MM-dd"));
                if (customerData==null){
                    showToast("请选择所属用户");
                }else if (StringUtils.isEmpty(money)){
                    showToast("请输入金额");
                }else if (type==null){
                    showToast("请选择状态");
                }else if (startTime==null){
                    showToast("请选择开始时间");
                }else if (handleTime==null){
                    showToast("请选择处理时间");
                }else {
                    customerData.setHandleDate(startTime);
                    if (type == BillData.Type.REPAY){
                        customerData.setType(CustomerData.Type.COMPLETE);
                    }else {
                        customerData.setType(CustomerData.Type.NO_EXPIRE);
                    }
                    customerDataBox.put(customerData);


                    BillData data = new BillData();
                    data.setMoney(Double.valueOf(money));
                    data.setStartData(startTime);
                    data.setHandleData(handleTime);
                    data.setType(type);
                    data.customerDataToOne.setTarget(customerData);
                    billBox.put(data);

                    finish();

                }
                break;
            default:
                break;
        }
    }


    //初始化时间选择器
    private void initPicker() {
        Calendar startDate = Calendar.getInstance();
        Calendar endDate = Calendar.getInstance();
        startDate.set(2018,1,0);
        endDate.set(2020,0,0);

        startPicker = new TimePickerBuilder(getContext(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                String startStr = TimeUtils.date2String(date,new SimpleDateFormat("yyyy-MM-dd"));
                tvStartTime.setRightString(startStr);
            }
        }).setType(new boolean[]{true,true,true,false,false,false})
                .setLabel("", "", "", "", "", "")
                .setDividerColor(ContextCompat.getColor(getContext(), R.color.divider))
                .setContentTextSize(20).setDate(Calendar.getInstance()).setRangDate(startDate,endDate);

        handlePicker = new TimePickerBuilder(getContext(), new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                String endString = TimeUtils.date2String(date,new SimpleDateFormat("yyyy-MM-dd"));
                tvHandleTime.setRightString(endString);
            }
        }).setType(new boolean[]{true,true,true,false,false,false})
                .setLabel("", "", "", "", "", "")
                .setDividerColor(ContextCompat.getColor(getContext(), R.color.divider))
                .setContentTextSize(20).setDate(Calendar.getInstance()).setRangDate(startDate,endDate);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CHECK_MEMBER:
                if (resultCode == RESULT_OK) {
                    customerData = data.getParcelableExtra("data");
                    tvMember.setRightString(customerData.getName());
                }
                break;
            default:
                break;
        }
    }


}
